Overview
This is a program I made for a job application, they hired someone else so I figured I’d make some use for it and put it up here. Personally I think it’s a rather nice program. The purpose of it is to display the numbers 1 to 10,000 in random order. The general approach is to create a data set containing the 10,000 numbers and then repeatedly generate random indexes to get the numbers from the set in random order while making sure that they are not repeated. The program uses a WPF window to provide the interface for the program.
Interface
The program has a fairly basic interface shown in figure 1. There is a list box to contain the numbers along with a progress bar and a label to indicate to the user the current status of the program. There is also a button to start the program generating numbers that can be pressed multiple times.
Interface (C# on left, Vb.net on right
When the user clicks on the button the program starts generating numbers and adding them to the list box. The progress bar is updated periodically as numbers are generated to indicate the percentage of numbers generated. The button is disabled after it is clicked to ensure that it’s not clicked again while the program is generating numbers. When the numbers are finished being generated the program resets the progress bar, re-enables the button, and displays a message box. The message box shows the actual sum of the generated numbers, the expected sum, the difference, and the elapsed time. The expected sum is calculated using the following formula and is used to ensure all numbers are generated as expected.
Equation for sum from i to n
Code
MainWindowCs.xaml.cs
The majority of the work done by the program is in the “generateNumbers” method which is ran in a background thread created by the click handler of the generate Button. This method starts by creating a list and then looping to add all 10,000 numbers to it. It then loops through the list again in order to display the numbers. It first generates a random number between 0 and the count of numbers in the list to use as an index. The index is used to get the number from the list it is then added to the listBox and removed from the list. This ensures that no number is displayed twice.
The progressSpace variable is used to determine when the progress bar should be updated. It is calculated by dividing the count of numbers to be generated by 100 to get the count of numbers in one percent. The progressAmount variable is used to determine how much to increase the value of the progress bar by each time it is updated and is initially set to 1. If the count of numbers is less than 100 the initial value of progressSpace will be 0. In this case progressSpace is set to 1 and the progressAmount variable is recalculated as 100 divided by the count of numbers. This is done because if the count of numbers is less than 100 it means a single number represents more than a single percent. The progress bar value is increased by progressAmount when the modulus (The remainder of the division) of the progressSpace and the loop index is 0.
The list was chosen for this program because it was deemed to be the best choice but it wasn’t the only possible option. Arrays and hashSets were also considered but testing showed they were less efficient. Arrays require less overhead than lists but there’s no built in remove method so to indicate that a number has already been displayed the program would have to set the value at the randomly generated index to 0. The program would then skip any indexes with a value of 0 in the array and generate a new index. Using hashSets the program would not need to initially fill the data set. Instead the program would randomly generate numbers and attempt to add them to the hashSet. If the number is added successful it means it doesn’t already exist in the hashSet and can be displayed otherwise it means the number has already been displayed and it should be skipped. In both cases extra loops are required to detect and skip repeated numbers. Ten tests were done with each type and the results are summarized in the table below.
The tests show that on average the list is the quickest data type. There are runs were the HashSet and Array are quicker than the list but because of the extra loops they have a much larger time span. The list type still has a fairly wide time span because of the complexity of the remove method but overall it seems to be the more efficient option.
Type | List | HashSet | Array |
Run 1 | 5.526 | 16.827 | 18.001 |
Run 2 | 10.481 | 6.161 | 4.191 |
Run 3 | 6.178 | 3.035 | 3.393 |
Run 4 | 4.255 | 10.099 | 17.116 |
Run 5 | 3.769 | 4.911 | 4.298 |
Run 6 | 10.295 | 5.625 | 15.328 |
Run 7 | 8.218 | 11.5 | 4.498 |
Run 8 | 5.632 | 7.977 | 7.186 |
Run 9 | 4.797 | 2.627 | 14.054 |
Run 10 | 8.697 | 8.399 | 3.714 |
Average | 6.785 | 7.716 | 9.178 |
Max | 10.481 | 16.827 | 18.001 |
Min | 3.769 | 2.627 | 3.393 |
MainWindowCs.xaml
This file describes the structure of the program’s interface. The ListBox is used to contain the list of random numbers while the ProgressBar and Label are used to display the current status of the program. The Button is used to start the program generating numbers. Several attributes of these controls are bound to properties of the corresponding C# code. The value attribute of the ProgressBar is bound to the ProgressValue property of the window and the Content attribute of the Label is bound to the StatusContent property. The ItemsSource attribute of the ListBox is set to binding but the target is set in code.
MainWindowVb.xaml.vb
This is a code translation of the Random Order program from C# to Visual basic .Net. Visual Basic is quite syntactically different compared to the C based languages I’m used to but it shares a lot of the same elements (Especially with C# as they are both .net languages). Because of this translating a program seemed the best way to learn VB as it meant I didn’t have to figure out a new problem just a new way of writing an already solved one. VB is actually a fairly easy language to use once you figure it out.
MainWindowVb.xaml
This is the XAML structure file for the visual basic version of the Random Order program. Because C# and VB.net are both .net languages they both use the same XAML detentions so there’s very little difference between this file and the one for the C# version. The only thing that was changed was the title attribute of the window which contains the language being used.